﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNet.Common.Kit;
using System.Net;
using System.IO;

namespace Shop.Image.Kit
{
    public class TaogonghuoImage:BaseImage
    {
        string _domain = "";
        string _ftpUserName = "";
        string _ftpPwd = "";
        WebClient client = new WebClient();
        LogerHelper log = new LogerHelper(DirType.Local, MessageType.Error);

        public TaogonghuoImage(string iniFilePath,string iniSection,string dirFileName)
        {
            IniHelper ini = new IniHelper(iniFilePath);
            _domain = ini.GetKeyValue(iniSection, "domain");
            _ftpUserName = ini.GetKeyValue(iniSection, "username");
            _ftpPwd = ini.GetKeyValue(iniSection, "pwd");
            client.Credentials = new NetworkCredential(_ftpUserName, _ftpPwd);
            if (_domain != "" && _ftpUserName != "" && _ftpPwd != "")
            {
                string strFileList = GetFileList();
                if (!strFileList.Contains(dirFileName))
                {
                    CreateDirFile(dirFileName);
                }
                DirFileName = dirFileName;
            }
            else
            {
                log.LogToFile("ini文件配置错误");
            }
        }

        /// <summary>
        /// 上传一张图片到空间中
        /// </summary>
        /// <param name="imgFullName">图片文件的全路径，请带有后缀，.jpg</param>
        /// <param name="imgName">图片更名后的文件名，请带有后缀，.jpg</param>
        /// <returns>成功返回 true 否则返回 false</returns>
        public override string ImageUpload(string imgFullName,string imgName)
        {
            string picUrl = "";
            try
            {
                client.UploadFile("ftp://" + _domain + "/" + DirFileName + "/" + imgName, imgFullName);
                picUrl = "http://" + _domain + "/" + DirFileName + "/" + imgName;
                return picUrl;
            }
            catch (Exception ex)
            {
                log.LogToFile(ex.Message + "\r\n" + ex.StackTrace);
                return picUrl;
            }
        }

        private string GetFileList()
        {
            try
            {
                FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+_domain));
                ftpReq.Credentials = new NetworkCredential(_ftpUserName, _ftpPwd);
                ftpReq.Method = WebRequestMethods.Ftp.ListDirectory;
                WebResponse response = ftpReq.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
                string line = reader.ReadToEnd();
                reader.Close();
                response.Close();
                return line;
            }
            catch (Exception ex)
            {
                log.LogToFile(ex.Message+"\r\n"+ex.StackTrace);
                return "";
            }
        }

        private bool CreateDirFile(string dirFileName)
        {
            try
            {
                FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + _domain+"//"+dirFileName));
                ftpReq.Credentials = new NetworkCredential(_ftpUserName, _ftpPwd);
                ftpReq.Method = WebRequestMethods.Ftp.MakeDirectory;
                WebResponse response = (FtpWebResponse)ftpReq.GetResponse();
                response.Close();
                return true;
            }
            catch (Exception ex)
            {
                log.LogToFile(ex.Message + "\r\n" + ex.StackTrace);
                return false;
            }
        }
    }
}
